<template>
  <div class="login">
    <img class="login-img" src="@/assets/svg/login.svg" />
    <div class="login-form">
      <div class="title">集装箱超偏载检测系统</div>
      <el-form ref="formRef" :model="model" :rules="rules" label-width="auto">
        <el-form-item label="手机号" prop="phone">
          <el-input placeholder="请输入手机号" v-model="model.phone" autocomplete="off" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            placeholder="请输入密码"
            v-model="model.password"
            type="password"
            autocomplete="off"
          />
        </el-form-item>
        <!-- <el-form-item label=" " prop="isRemember">
          <el-checkbox v-model="model.isRemember" />
          &nbsp;记住密码
        </el-form-item> -->
      </el-form>
      <el-button class="button" type="primary" @click="handleLogin(formRef)">登录</el-button>
      <div class="login-forget">忘记密码</div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElForm, ElFormItem, ElInput, ElCheckbox, ElButton, ElMessage } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'
import http from '@/utils/request'
import { useUserStore } from '@/stores/user'

const router = useRouter()
const userStore = useUserStore()
const formRef = ref<FormInstance>()
const model = reactive({
  phone: '',
  password: '',
  isRemember: false
})

const rules: FormRules = {
  phone: {
    required: true,
    message: '请输入手机号'
  },
  password: {
    required: true,
    message: '请输入密码'
  }
}

const handleLogin = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate(async (errors) => {
    if (errors) {
      const { phone, password } = model
      const params = {
        phone,
        password,
        userType: 0
      }
      const res = await http.post('/login/token', params)
      if (res.code === '200') {
        const { token, userId } = res.result
        sessionStorage.setItem('token', token)
        const userinfo = JSON.stringify({ userId, phone })
        sessionStorage.setItem('userinfo', userinfo)
        userStore.changeUserinfo({ userId, phone })
        router.push({ path: '/' })
      } else {
        ElMessage({
          message: res.desc || '用户名不存在或密码错误！',
          type: 'error'
        })
      }
    } else {
      ElMessage({
        message: '请完善信息',
        type: 'warning'
      })
    }
  })
}
</script>

<style scoped lang="scss">
.login {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  &-img {
    position: absolute;
    top: 0;
    left: 0;
    z-index: -1;
  }
  &-form {
    padding: 20px;
    width: 400px;
    border: 1px solid #eee;
    border-radius: 4px;
    display: flex;
    flex-direction: column;
    align-items: center;
    .title {
      margin: 30px 0;
      font-size: 20px;
      font-weight: bold;
    }
    .button {
      width: 258px;
    }
    .login-forget {
      width: 258px;
      display: flex;
      justify-content: flex-end;
      margin-top: 16px;
      color: #6c6c6c;
      font-size: 12px;
      cursor: pointer;
    }
  }
}
</style>
